//
//  GHTabBarController.m
//  12-itcastweibo-1
//
//  Created by GHapple on 15-1-28.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "GHTabBarController.h"
#import "GHTabBar.h"

@interface GHTabBarController ()

@end

@implementation GHTabBarController



- (void)viewDidLoad{
    [super viewDidLoad];
//     替换自身的tabbar
    GHTabBar *mytabbar = [[GHTabBar alloc]init];
    // 因为是只读的,所以不能直接赋值,需要通过kvc来赋值
    [self setValue:mytabbar forKey:@"tabBar"];

}

// 在此方法里面初始化子控制器
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // 初始化
        UIViewController *home = [self addController:[[UIViewController alloc]init] title:@"首页" imageName:@"tabbar_home" selectedImage:@"tabbar_home_selected"];
        UIViewController *message = [self addController:[[UIViewController alloc]init] title:@"消息" imageName:@"tabbar_message_center" selectedImage:@"tabbar_message_center_selected"];
        UIViewController *discover = [self addController:[[UIViewController alloc]init] title:@"发现" imageName:@"tabbar_discover" selectedImage:@"tabbar_discover_selected"];
        UIViewController *profile = [self addController:[[UIViewController alloc]init] title:@"我" imageName:@"tabbar_profile" selectedImage:@"tabbar_profile_selected"];
        self.viewControllers = @[home,message,discover,profile];
    }
    return self;
}

- (UIViewController *)addControllerWithClass:(Class)class
                             title :(NSString *)title imageName:(NSString *)imagename selectedImage:(NSString *)selectedImage{
    UIViewController *vc = [[class alloc]init];
    
    return [self addController:vc title:title imageName:imagename selectedImage:selectedImage];

}

- (UIViewController *)addController:(UIViewController *)vc
                             title :(NSString *)title imageName:(NSString *)imagename selectedImage:(NSString *)selectedImage{
    vc.tabBarItem.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:imagename];
    // 如果是iOS7 要设置不要渲染
    
    UIImage *newImage =[UIImage imageNamed:selectedImage];
    newImage = [newImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = newImage;
    [vc.view setBackgroundColor:KWColor];
    // 设置文字的样式
    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
    attr[NSForegroundColorAttributeName]= [UIColor orangeColor];
    [vc.tabBarItem setTitleTextAttributes:attr forState:UIControlStateSelected];
    return  vc;
}




@end
